//
//  WaveformGraphView.m
//  安好
//
//  Created by qinlongqing on 14-9-1.
//  Copyright (c) 2014年 冯凯. All rights reserved.
//

#import "WaveformGraphView.h"

//static const NSInteger kMaxContainerCapacity = 320;

static NSInteger currentPointsCount;


@interface PointContainer ()
{
    NSInteger kMaxContainerCapacity;
}

@property (nonatomic) NSInteger numberOfTranslationElements;//统计多少个，最多为kMaxContainerCapacity的个数

@property (nonatomic) CGPoint *translationPointContainer;//这里是指向CGPoint类型变量的指针(数组)

@end

@implementation PointContainer

- (void)initDataWithWidth:(CGFloat)width{
    //创建数组所需的内存，并初始化
    currentPointsCount = 0;
    
    kMaxContainerCapacity = width;
    
    self.translationPointContainer = malloc(sizeof(CGPoint) * kMaxContainerCapacity);
    memset(self.translationPointContainer, 0, sizeof(CGPoint) * kMaxContainerCapacity);
}

- (void)addPointAsTranslationChangeForm:(CGPoint)point{
#pragma 从左到右
    if (currentPointsCount <= kMaxContainerCapacity/4) {
        self.numberOfTranslationElements = currentPointsCount + 1;
        self.translationPointContainer[currentPointsCount] = point;
        currentPointsCount++;
    }
    else{
        NSInteger workIndex = kMaxContainerCapacity/4;
        while (workIndex != 0) {
            self.translationPointContainer[workIndex].y = self.translationPointContainer[workIndex - 1].y;
            workIndex--;
        }
        self.translationPointContainer[0].x = 0;
        self.translationPointContainer[0].y = point.y;
        self.numberOfTranslationElements = kMaxContainerCapacity/4;
    }
}

- (void)dealloc{
    NSLog(@"PointContainer dealloc");
}

@end



//----------------恶心的分割线-------------------
@interface WaveformGraphView ()

@property (nonatomic) CGPoint *points;

@property (nonatomic) NSInteger currentPointsCount;

@end

@implementation WaveformGraphView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (void)setPoints:(CGPoint *)points{
    _points = points;
    [self setNeedsDisplay];
}

- (void)fireDrawingWithPoints:(CGPoint *)points pointsCount:(NSInteger)count{
    self.currentPointsCount = count;
    self.points = points;
//    NSLog(@"*******currentPointsCount = %d",count);
}

// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
    [self drawCurve];
}

- (void)drawCurve{
//    /*从右到左
    if (self.currentPointsCount == 0) {
        return;
    }
    CGContextRef context = UIGraphicsGetCurrentContext();
    CGContextSetLineWidth(context, 0.8);
    CGContextSetStrokeColorWithColor(context, [UIColor redColor].CGColor);
    
    CGContextMoveToPoint(context, self.points[0].x, self.points[0].y);
    for (int i=1; i <= self.currentPointsCount; i++) {
        if (self.points[i - 1].x < self.points[i].x) {
            CGContextAddLineToPoint(context, self.points[i].x, self.points[i].y);
        }
        else{
            CGContextMoveToPoint(context, self.points[i].x, self.points[i].y);
        }
    }
    CGContextStrokePath(context);
}

- (void)dealloc{
    NSLog(@"waveform graph view dealloc");
}

@end
